package com.gmail.at.dengtooling.util;

/**
 * 
 * @author Deng Tooling
 * @version 1.0
 */
public class Type {

	/**
	 * convert a bytes array to int
	 * 
	 * @param b
	 *            bytes array
	 * @return an int value of b
	 */
	public static int bytes2int(byte[] b) {
		int mask = 0xff;
		int temp = 0;
		int res = 0;
		for (int i = 0; i < 4; i++) {
			res <<= 8;
			temp = b[i] & mask;
			res |= temp;
		}
		return res;
	}

	/**
	 * convert a bytes array to int
	 * 
	 * @param b
	 *            bytes array
	 * @param pos
	 *            start position
	 * @return int value of b
	 */
	public static int bytes2int(byte[] b, int pos) {
		int mask = 0xff;
		int temp = 0;
		int res = 0;
		for (int i = pos; i < 4 + pos; i++) {
			res <<= 8;
			temp = b[i] & mask;
			res |= temp;
		}
		return res;
	}

	public static byte[] int2bytes(int num) {
		byte[] b = new byte[4];
		for (int i = 0; i < 4; i++) {
			b[i] = (byte) (num >>> (24 - i * 8));
		}
		return b;
	}
}
